package org.restql.db.operation;

import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.restql.db.resource.DatabaseResource;
import org.restql.db.resource.RequestResource;
import org.restql.webapp.RestQLException;
import org.restql.webapp.ReturnType;

public abstract class DatabaseGet extends DatabaseOperation {

	public DatabaseGet(DatabaseResource databaseResource, RequestResource requestResource) {
		super(databaseResource, requestResource);
	}

	private static Log logger = LogFactory.getLog(DatabaseGet.class);

	public abstract void executeSql(PrintWriter printer, ReturnType retType, String show) throws RestQLException;

	protected Map<String, String> getColInfo(ResultSet rs) throws SQLException {
		ResultSetMetaData colInfo = rs.getMetaData();
		Map<String, String> colTypes = new Hashtable<String, String>();

		for (int i = 1; i <= colInfo.getColumnCount(); i++) {
			if (!colInfo.getColumnName(i).startsWith ("RESTQL_TMP__")) {
				colTypes.put(colInfo.getColumnName(i), colInfo.getColumnTypeName(i));
			}
		}

		logger.info(colTypes);
		return colTypes;
	}

}
